package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.QuerySellerProductBean;
import com.ymatou.iapi.productmanager.parameter.QuerySellerProductListResponseBean;
import com.ymatou.iapi.productmanager.parameter.TabStatus;
import com.ymatou.iapi.productmanager.service.QuerySellerProductListCall;
import com.ymatou.iapi.productmongosync.service.EsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.business.service.productdata.impl.ProductFromJSONBean;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_QuerySellerProductList {
	private static QuerySellerProductListCall querySellerProductListCall = new QuerySellerProductListCall();
	private static EsCall esCall = new EsCall();
	
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	
	
	
	
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	//private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	
	private static String jsonBeanFile = "com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Search.json";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品列表查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querySellerProductListCall.getOpurl());
	}
	
	

	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_001() {
		Logger.start(true,"查询商品,根据ProductName");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();

			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			//商品名不对，应该搜不到
			querySellerProductBean.setProductName(productName+"22");
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "商品名不对，应该搜不到");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_002() {
		Logger.start(true,"查询商品,根据ProductName,品牌");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
		
			String brandName = ymtProductsIWapper.selectProductByProductId(productId).get(0).getsBrand();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setBrandName(brandName);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			//品牌名不对，应该搜不到
			querySellerProductBean.setBrandName("fasdf");
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "品牌名不对，应该搜不到");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_002_001() {
		Logger.start(true,"查询商品,根据ProductName,品牌Id");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
		
			int brandId = ymtProductsIWapper.selectProductByProductId(productId).get(0).getiBrandId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setBrandId(Arrays.asList(brandId));
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			
		
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->iBrandId",10145);
			String productId2 =  ProductFactory.getOnlyProduct(sellerId, sellerName,
					new ProductFromJSONBean.Builder().UpdateMap(updateMap).JsonBeanFile(jsonBeanFile).build()).getProductId();
			
			
			querySellerProductBean.setBrandId(Arrays.asList(brandId,10145));
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId,productId2));
			
			
			//品牌名不对，应该搜不到
			querySellerProductBean.setBrandId(Arrays.asList(1123));
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "品牌Id不对，应该搜不到");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_003() {
		Logger.start(true,"查询商品,根据ProductName,库存");
		try {
			
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setStockEnd(1000);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			//库存不在范围，应该搜不到
			querySellerProductBean.setStockEnd(998);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "库存不在范围，应该搜不到");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_004() {
		Logger.start(true,"查询商品,根据ProductName,是否在直播中");
		try {
			
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName);
			String productId =  ProductFactory.getLiveProduct(sellerId, sellerName,updateMap).getProductId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setIsInLive(true);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
						
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_005() {
		Logger.start(true,"查询商品,根据ProductName,销售价格");
		try {
			
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);

			
			//原价
			querySellerProductBean.setPriceType(1);
			querySellerProductBean.setPriceStart(300);
			querySellerProductBean.setPriceEnd(500);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			//价格不在范围，应该搜不到
			querySellerProductBean.setPriceStart(401);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "价格不在范围，应该搜不到");
			
			querySellerProductBean.setPriceStart(20);
			querySellerProductBean.setPriceEnd(49);
			JSONObject ret2 = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret2.isNull("Data"), "价格不在范围，应该搜不到");
			
			
			
			//new price
			querySellerProductBean.setPriceType(2);
			querySellerProductBean.setPriceStart(49);
			querySellerProductBean.setPriceEnd(51);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			//vip price
			querySellerProductBean.setPriceType(3);
			querySellerProductBean.setPriceStart(19);
			querySellerProductBean.setPriceEnd(21);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
						
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_006() {
		Logger.start(true,"查询商品,根据ProductName,是否是FBX");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("UserID",sellerId,"Product->ProductName",productName);
			String productId = SellerProductCallService.addFBXProduct(updateMap);
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setIsFbx(true);
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_007() {
		Logger.start(true,"查询商品,根据ProductName,预售");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setIsPreSale(true);
			
			//没有预售搜不到
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "库存不在范围，应该搜不到");
			
			
			//有预售
			ymtCatalogsIWapper.updatePreSaleByProductId(1, productId);
			esCall.callAndGetReturnData(0, productId, "AddProduct");
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	//todo
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_008() {
		Logger.start(true,"查询商品,根据ProductName,物流方式");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			//指定搜索,不在范围内,搜不到
			querySellerProductBean.setCatalogStatusList(Arrays.asList(1,2));
			
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "物流方式不在范围，应该搜不到");
			
			querySellerProductBean.setCatalogStatusList(Arrays.asList(1,2,3));
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			querySellerProductBean.setCatalogStatusList(Arrays.asList(1,2,3,0));
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			querySellerProductBean.setCatalogStatusList(Arrays.asList(1,2,0));
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_009() {
		Logger.start(true,"查询商品,根据ProductName,psp");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			//不是psp,不在范围内,搜不到
			querySellerProductBean.setIsPspProductOnly(true);
			
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "不是psp,不在范围内,搜不到");
			
			SellerProductCallService.setProductPsp(productId, true);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_010() {
		Logger.start(true,"查询商品,根据ProductName,品类");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			List<Integer> catagorysList = new ArrayList<>();
			
			//品类不在范围内,搜不到
			catagorysList.add(1233);
			querySellerProductBean.setCategoryIds(catagorysList);
			
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "不是psp,不在范围内,搜不到");
			
			
			
			//在三级范围内
			catagorysList.add(1123);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			
			//在二级范围内
			catagorysList.clear();
			catagorysList.add(1231);
			catagorysList.add(1120);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_011() {
		Logger.start(true,"查询商品,根据ProductName,新品");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			//新品查询
			querySellerProductBean.setIsNewProduct(true);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			//不是新品 不在范围内
			//品类不在范围内,搜不到
			ymtProductsIWapper.updateIsNewByUserId(sellerId, false);
			esCall.callAndGetReturnData(0, productId, "AddProduct");
			YMTDateUtil.waitTime(3);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "不是新品,不在范围内,搜不到");
			
		}catch(Exception e){
			Logger.fail(e);   
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_012() {
		Logger.start(true,"查询商品,根据ProductName,排序");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("UserID",sellerId,"Product->ProductName",productName);
			
			List<String> productIdList = new ArrayList<>();
			
			for (int i=0;i<5;i++){
				productIdList.add(ProductFactory.getOnlyProduct(sellerId, productName, updateMap).getProductId());
				YMTDateUtil.waitTime(2);
			}
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			
			
			List<String> actProductList = new ArrayList<>();
			JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
			for (int i=0;i<prodListAry.length();i++){
				actProductList.add(prodListAry.getJSONObject(prodListAry.length()-i-1).getString("ProductId"));
			}
			
			
			
			for (int i=0;i<productIdList.size();i++){
				Logger.verifyEquals(productIdList.get(i), actProductList.get(i), "第"+i+"个相同");
			}
			
		}catch(Exception e){
			Logger.fail(e);   
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_013() {
		Logger.start(true,"查询商品,根据ProductName,过滤橱窗商品");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			//新品查询
			querySellerProductBean.setFilterIsTop(true);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			//修改为橱窗商品，所以无法查询到
			SellerProductCallService.removeAllProductInTop(sellerId);
			SellerProductCallService.addProductsIntoTopList(sellerId, Arrays.asList(productId));
			YMTDateUtil.waitTime(3);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "修改为橱窗商品，所以无法查询到");
			
		}catch(Exception e){
			Logger.fail(e);   
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_014() {
		Logger.start(true,"查询商品,根据ProductName,分页个数");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("UserID",sellerId,"Product->ProductName",productName);
			
			List<String> productIdList = new ArrayList<>();
			
			for (int i=0;i<4;i++){
				productIdList.add(ProductFactory.getOnlyProduct(sellerId, productName, updateMap).getProductId());
				YMTDateUtil.waitTime(2);
			}
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setPageSize(2);

			//第一页查询
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			
			JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, prodListAry.length(), "商品显示数量为5");
			for (int i=0;i<2;i++){
				Logger.verifyEquals(productIdList.get(3-i), prodListAry.getJSONObject(i).getString("ProductId"), "第"+i+"个相同");
			}
			
			//第二页查询
			querySellerProductBean.setPageIndex(2);
			JSONObject ret2 = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			
			JSONArray prodListAry2 = ret2.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, prodListAry2.length(), "商品显示数量为5");
			for (int i=0;i<2;i++){
				Logger.verifyEquals(productIdList.get(1-i), prodListAry2.getJSONObject(i).getString("ProductId"), "第"+i+"个相同");
			}
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);   
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_015() {
		Logger.start(true,"查询商品,根据ProductName,Tab=0(销售中的商品,直播商品)");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getLiveProduct(sellerId, sellerName, updateMap).getProductId();
			int liveId = Integer.parseInt(ymtProductsInLiveWapper.selectInLiveByProductId(productId).get(0).get("iActivityId").toString());
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			querySellerProductBean.setLiveId(liveId);
			querySellerProductBean.setTabType(TabStatus.InLive.getCode());
			Logger.verifyEquals(true, querySellerProductListCall.callAndGetReturnData(querySellerProductBean).isNull("Data"), "不应该在tab:"+TabStatus.InLive);
			
			//暂停直播
			SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId);
			
			querySellerProductBean.setLiveId(0);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
			
			
			querySellerProductBean.setLiveId(liveId);
			querySellerProductBean.setTabType(TabStatus.InLive.getCode());
			Logger.verifyEquals(true, querySellerProductListCall.callAndGetReturnData(querySellerProductBean).isNull("Data"), "不应该在tab:"+TabStatus.InLive);
			
			//商品上架
			querySellerProductBean.setLiveId(0);
			SellerProductCallService.batchSetProductOnSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	//error
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_016() {
		Logger.start(true,"查询商品,根据ProductName,Tab=0(销售中的商品,活动商品)");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getActivityProduct(sellerId, sellerName, updateMap).getProductId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//现货售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//活动库存售罄
			SellerProductCallService.updateActivityStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_017() {
		Logger.start(true,"查询商品,根据ProductName,Tab=0(销售中的商品,现货商品)");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName);
			String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName, updateMap).getProductId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			//商品下架
			SellerProductCallService.batchSetProductOffSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
						
			//商品售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_018() {
		Logger.start(true,"查询商品,根据ProductName,Tab=4(待上架,将来活动商品,现货下架),");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getFutureActivityProduct(sellerId, sellerName, updateMap).getProductId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
			
			
			//现货上架
			SellerProductCallService.batchSetProductOnSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//现货售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_019() {
		Logger.start(true,"查询商品,根据ProductName,Tab=4(待上架,已结束活动,现货下架),");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getPreviousActivityProduct(sellerId, sellerName, updateMap).getProductId();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
			
			//现货上架
			SellerProductCallService.batchSetProductOnSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//现货售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_020() {
		Logger.start(true,"查询商品,根据ProductName,Tab=4(待上架,将来直播商品,现货下架),");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getFutureLiveProduct(sellerId, sellerName, updateMap).getProductId();
			int liveId = Integer.parseInt(ymtProductsInLiveWapper.selectInLiveByProductId(productId).get(0).get("iActivityId").toString());
			
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
			
			
			//现货上架
			SellerProductCallService.batchSetProductOnSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//现货售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
			
			//排除该直播
			querySellerProductBean.setLiveId(liveId);
			querySellerProductBean.setTabType(TabStatus.InLive.getCode());
			Logger.verifyEquals(true, querySellerProductListCall.callAndGetReturnData(querySellerProductBean).isNull("Data"), "不应该在tab:"+TabStatus.InLive);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_021() {
		Logger.start(true,"查询商品,根据ProductName,Tab=4(待上架,已结束直播商品,现货下架),");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName,"Product->OnSaleType", 2,"Product->AutoRefresh",false);
			String productId =  ProductFactory.getPreviousLiveProduct(sellerId, sellerName, updateMap).getProductId();
			int liveId = Integer.parseInt(ymtProductsInLiveWapper.selectInLiveByProductId(productId).get(0).get("iActivityId").toString());
			
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnNotSale,TabStatus.InLive));
			
			
			//现货上架
			SellerProductCallService.batchSetProductOnSale(productId, sellerId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.OnSale,TabStatus.InLive));
			
			
			//现货售罄
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId);
			YMTDateUtil.waitTime(3);
			checkInTab(sellerId,productId,querySellerProductBean,Arrays.asList(TabStatus.SaleOut,TabStatus.InLive));
			
			
			//排除该直播
			querySellerProductBean.setLiveId(liveId);
			querySellerProductBean.setTabType(TabStatus.InLive.getCode());
			Logger.verifyEquals(true, querySellerProductListCall.callAndGetReturnData(querySellerProductBean).isNull("Data"), "不应该在tab:"+TabStatus.InLive);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_022() {
		Logger.start(true,"查询商品,根据ProductName,SortType");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
		
		
			YMTDateUtil.waitTime(3);
			Pair<String,String> ProductIdAndName2 = getOnlyProduct(productName);
			String productId2 = ProductIdAndName2.getLeft();
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			
			
			//时间倒叙 setSortType -1
			YMTDateUtil.waitTime(3);
			querySellerProductBean.setSortType(0);
			querySellerProductBean.setSort(0);
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			JSONArray productList = ret.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, productList.length(), "返回productList长度");
			Logger.verifyEquals(productId2, productList.getJSONObject(0).getString("ProductId"), "返回商品Id");
			Logger.verifyEquals(productId, productList.getJSONObject(1).getString("ProductId"), "返回商品Id");
			
			//时间正序 setSortType -1
			YMTDateUtil.waitTime(3);
			querySellerProductBean.setSortType(0);
			querySellerProductBean.setSort(1);
			ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			productList = ret.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, productList.length(), "返回productList长度");
			Logger.verifyEquals(productId2, productList.getJSONObject(1).getString("ProductId"), "返回商品Id");
			Logger.verifyEquals(productId, productList.getJSONObject(0).getString("ProductId"), "返回商品Id");
			
			///////////////////////////////////////////////////////////////////////////////////////////////////
			String catalogId = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getsCatalogId();
			SellerProductCallService.updateProductStockNum(sellerId, productId, catalogId, 10000);
			
			//库存倒叙 setSortType >0
			YMTDateUtil.waitTime(3);
			querySellerProductBean.setSortType(1);
			querySellerProductBean.setSort(0);
			ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			productList = ret.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, productList.length(), "返回productList长度");
			Logger.verifyEquals(productId2, productList.getJSONObject(1).getString("ProductId"), "返回商品Id");
			Logger.verifyEquals(productId, productList.getJSONObject(0).getString("ProductId"), "返回商品Id");
			
			
			//库存正序 setSortType >0
			YMTDateUtil.waitTime(3);
			querySellerProductBean.setSortType(1);
			querySellerProductBean.setSort(1);
			ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			productList = ret.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2, productList.length(), "返回productList长度");
			Logger.verifyEquals(productId2, productList.getJSONObject(0).getString("ProductId"), "返回商品Id");
			Logger.verifyEquals(productId, productList.getJSONObject(1).getString("ProductId"), "返回商品Id");
			

		
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_023() {
		Logger.start(true,"查询商品,根据ProductName,PSP");
		try {
			Pair<String,String> ProductIdAndName = getOnlyProduct();
			String productName = ProductIdAndName.getRight();
			String productId = ProductIdAndName.getLeft();
		
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(sellerId);
			querySellerProductBean.setProductName(productName);
			querySellerProductBean.setPspStatus(1);
			
		
			//不是psp，应该搜不到
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "不是psp，应该搜不到");
			
			
			//设置商品为psp 待优化
			SqlMapperFactory.getIntergratedProductMapper().updatePspByProductId(productId, true);
			SqlMapperFactory.getIntergratedProductMapper().insertProductInfo(productId, sellerId);
			ProductManagerCallService.callMgoAndEs(0, productId, "AddProduct");
			
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
			
			querySellerProductBean.setPspStatus(0);
			checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
			
			
		
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySellerProductList_088() {
		Logger.start(true,"查询商品,根据ProductName,非该用户无法查商品");
		try {
			String productName = "测试商品"+TestDataManager.getRandomNum(10);
			Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName);
			ProductFactory.getOnlyProduct(sellerId, sellerName,updateMap).getProductId();
			
			
			QuerySellerProductBean querySellerProductBean =  new QuerySellerProductBean();
			querySellerProductBean.setSellerId(3123);
			querySellerProductBean.setProductName(productName);
			
			JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
			Logger.verifyEquals(true, ret.isNull("Data"), "非该用户无法查商品");
			
		}catch(Exception e){
			Logger.fail(e);   
		}
	}
	
	
	
	private static Pair<String,String> getOnlyProduct(){
		String productName = "测试商品"+TestDataManager.getRandomNum(10);
		Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName);
		String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName,
				new ProductFromJSONBean.Builder().UpdateMap(updateMap).JsonBeanFile(jsonBeanFile).build()).getProductId();
		return Pair.of(productId, productName);
	}
	
	
	private static Pair<String,String> getOnlyProduct(String productName){
		Map<String,Object> updateMap = MapUtil.hashMap("Product->ProductName",productName);
		String productId =  ProductFactory.getOnlyProduct(sellerId, sellerName,
				new ProductFromJSONBean.Builder().UpdateMap(updateMap).JsonBeanFile(jsonBeanFile).build()).getProductId();
		return Pair.of(productId, productName);
	}
	
	
	
	public static void checkQuerySellerProductList(int sellerId, QuerySellerProductBean querySellerProductBean,List<String> tgtProductList) throws Exception{
		YMTDateUtil.waitTime(3);
		JSONObject ret = querySellerProductListCall.callAndGetReturnData(querySellerProductBean);
		
		if (ret.isNull("Data")){
			Logger.verifyEquals(false, true, "没有查到商品数据");
			return;
		}
		QuerySellerProductListResponseBean actBean = ProductManagerCallService
										.getBeanFromJson(ret.getJSONObject("Data"), 
												QuerySellerProductListResponseBean.class,"yyyy-MM-dd HH:mm:ss");
		
		
		
		QuerySellerProductListResponseBean tgtBean = ProductManagerCallService.getQuerySellerProductListResponseBean(tgtProductList);
		
		Logger.verifyEquals(actBean.getProductList().size(), actBean.getProductList().size(), "查询结果应与期望值相同");
		AssertHelper.assertResultEqual(tgtBean, actBean, "QuerySellerProductList");
		
		
		
	}
	
	
	
	public static void checkInTab(int sellerId,String productId,QuerySellerProductBean querySellerProductBean,List<TabStatus> isInTabList) throws Exception{
		List<TabStatus> allTabStatus = Arrays.asList(TabStatus.SaleOut,TabStatus.OnSale,TabStatus.OnNotSale,TabStatus.InLive);
		
		List<TabStatus> notInTabList;
		
		if (isInTabList!=null &&isInTabList.size()>0){
			isInTabList.forEach(ele->{
				Logger.comment("======应该在tab:"+ele+"========");
				querySellerProductBean.setTabType(ele.getCode());
				try {
					checkQuerySellerProductList(sellerId,querySellerProductBean,Arrays.asList(productId));
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			notInTabList = allTabStatus.stream().filter(x->!isInTabList.contains(x)).collect(Collectors.toList());
		}else{
			notInTabList = allTabStatus;
		}
		
		
		
		notInTabList.forEach(ele->{
			Logger.comment("=====不应该在tab:"+ele+"=======");
			querySellerProductBean.setTabType(ele.getCode());
			Logger.verifyEquals(true, querySellerProductListCall.callAndGetReturnData(querySellerProductBean).isNull("Data"), "不应该在tab:"+ele);
		});
	}
	
	
}
